package com.gxg.controller;


import com.gxg.entities.User;
import com.gxg.services.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class VideoController {

    @Autowired
    VideoService videoService;

    @PostMapping({"/get_all_video_list"})
    public String getAllVideoList(){
        return videoService.getAllVideoList();
    }


    @PostMapping({"/get_my_video_list"})//获取当前教师的所有视频
    public String getMyVideoList(@RequestParam ("teacherId") String teacherId){
        return videoService.getMyVideoList(teacherId);
    }


    @PostMapping("/uploadVideo")
    public String uploadVideo(@RequestParam("parentTitle") String parentTitle,
                              @RequestParam("childTitle") String childTitle,
                                           @RequestParam("videoClass") String videoClass,
                                           @RequestParam("video") MultipartFile video,
                                            @RequestParam(value = "parentImg",required = false) MultipartFile parentImg,
                              HttpServletRequest request
        ) throws Exception {

        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        session.setAttribute("user", user);
        String teacherId = user.getId();
        String res = videoService.videoUpload(parentTitle,childTitle,teacherId,videoClass,video,parentImg);
        return res;
    }

    /**
     * 通过本地文件路径删除视频
     * @param localUrl
     * @param imgLocalUrl
     * @return
     */
    @PostMapping("/deleteByLocalUrl")
    public String deleteByLocalUrl(@RequestParam("localUrl") String localUrl,
                                   @RequestParam("imgLocalUrl") String imgLocalUrl){
        return videoService.deleteByLocalUrl(localUrl,imgLocalUrl);
    }

    /**
     * 通过视频的路径获取老师的Id
     * @param webUrl
     * @return
     */
    @GetMapping("/getVideoByWebUrl")
    public String getTeacherIdByWebUrl(String webUrl){
        return videoService.getVideoByWebUrl(webUrl);
    }

    /**
     * 获取视频总页数
     * @return
     */
    @PostMapping("/getSumVideoPageCount")
    public Integer getSumVideoPageCount(){
        return videoService.getSumVideoPageCount();
    }

    /**
     * 获取当前页面的视频
     * @param currPageNumber
     * @return
     */
    @PostMapping("/getPageVideoByPageNumber")
    public String getPageVideoByPageNumber(@RequestParam("currPageNumber") Integer currPageNumber){
        String currPageVideoInfo = videoService.getPageVideoByPageNumber(currPageNumber);
        return currPageVideoInfo;
    }

    /**
     * 获取老师当前页面的视频
     * @param teacherId
     * @param currPageNumber
     * @return
     */
    @PostMapping("/getTeacherPageVideo")
    public String getTeacherPageVideo(@RequestParam("teacherId") String teacherId, @RequestParam("currPageNumber") Integer currPageNumber){
        String currPageVideoInfo = videoService.getTeacherPageVideo(teacherId,currPageNumber);
        return currPageVideoInfo;
    }

    /**
     * 获取老师视频总数
     * @param teacherId
     * @return
     */
    @PostMapping("/getTeacherVideoSumNumber")
    public Integer getTeacherVideoSumNumber(@RequestParam("teacherId") String teacherId){
        return videoService.getTeacherVideoSumNumber(teacherId);
    }

    /**
     * 获取某类的视频视频总数
     * @param videoClass
     * @return
     */
    @PostMapping("/getVideoSumNumberByVideoClass")
    public Integer getVideoSumNumberByVideoClass(@RequestParam("videoClass") String videoClass){
        return videoService.getVideoSumNumberByVideoClass(videoClass);
    }

    /**
     * 获取当前页的某类视频
     * @param videoClass
     * @param pageNumber
     * @return
     */
    @PostMapping("/getClassVideo")
    public String getClassVideo(@RequestParam("videoClass") String videoClass, @RequestParam("pageNumber") Integer pageNumber){
        return videoService.getClassVideo(videoClass,pageNumber);
    }

    @GetMapping("/getParentVideo")
    public String getParentVideo(@RequestParam("pageNumber") Integer pageNumber,@RequestParam(value = "teacherId",required = false) String teacherId,
                                 @RequestParam(value = "videoClass",required = false) String videoClass,
                                 @RequestParam(value = "parentTitle",required = false) String parentTitle){
        return videoService.getParentVideo(teacherId,pageNumber,videoClass,parentTitle);
    }

    @GetMapping("/getVideoByTeacherIdAndParentTitle")
    public String getVideoByTeacherIdAndParentTitle(String teacherId,Integer pageNumber,String parentTitle){
        return videoService.getVideoByTeacherIdAndParentTitle(teacherId,parentTitle,pageNumber);
    }
    @GetMapping("/getParentVideoSumNumber")
    public String getParentVideoSumNumber(@RequestParam(value = "videoClass",required = false) String videoClass){
        return videoService.getSumNumber(videoClass);
    }

    @GetMapping("/getChildVideoSumNumber")
    public String getChildVideoSumNumber(String teacherId,String parentTitle){
        return videoService.getChildVideoSumNumber(teacherId,parentTitle);
    }

    @GetMapping("/getParentVideoSumNumberByTeacherId")
    public String getParentVideoSumNumberByTeacherId(String teacherId){
        return videoService.getParentVideoSumNumberByTeacherId(teacherId);
    }

    @GetMapping("/getParentVideoByTeacherId")
    public String getParentVideoByTeacherId(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        session.setAttribute("user", user);
        String teacherId = user.getId();
        return videoService.getParentVideoByTeacherId(teacherId);
    }
}
